package com.dmm.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmm.Exception.Exceptionbean;
import com.dmm.user.entity.Signin;
import com.dmm.user.entity.Userinfo;
import com.dmm.user.service.SigninService;
import com.dmm.result.Result;
import com.dmm.user.service.UserinfoService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dmm
 * @since 2023-03-27
 */
@RestController
@CrossOrigin  //跨域问题
@RequestMapping("/user/signin")
public class SigninController {

    @Autowired
    SigninService signinService;

    @Autowired
    UserinfoService userinfoService;


    @PostMapping("login")
    public Result login(@RequestBody Signin signin){

        System.out.println(signin.toString());
        QueryWrapper<Signin> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq( "email",signin.getEmail());
        try {
            Signin one = signinService.getOne(queryWrapper);
            System.out.println(one.toString());
            if(!one.getPassword().equals(signin.getPassword())){
                throw new Exceptionbean(20001,"密码不正确");
            }

            Map<String, Object> map = new HashMap<>();
            map.put("token",one.getEmail());
  //          map.put("token","admin");
            return Result.success(map);

        }catch(Exception e) {
            throw new Exceptionbean(20001,"账号或密码不正确");
        }

    }

    @GetMapping("/getInfo")
    public Result getInfo(@RequestParam(value = "token") String str, HttpServletRequest request){

//        String token = request.getHeader("token");
//        System.out.println("token="+token);
//        System.out.println(str);
        Map<String, Object> map = new HashMap<>();
        map.put("roles","[admin]");
        map.put("name",str);
        map.put("avatar","https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
        map.put("introduction ","null");
        return Result.success(map);
    }

    @PostMapping("logout")
    public Result logout(){
        return Result.success(null);
    }



    //获取验证码
    @PostMapping("getEmailCode")
    public Result getEmailCode(@RequestBody Signin signin){

        signinService.EmailCode(signin.getEmail());

        return Result.success(null);
    }

    @PostMapping("register")
    public Result register( @RequestBody Signin signin){



        signinService.registeremail(signin);


        return Result.success(null);
    }


    @PostMapping("/Change")
    public Result Change(@RequestBody Signin signin,
                         HttpServletRequest request){

        String email = request.getHeader("token");
        QueryWrapper<Signin> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq( "email",email);
        Signin one = signinService.getOne(queryWrapper);
        if((one.getPassword().equals(signin.getOldpassword()))){
            one.setPassword(signin.getPassword());
            signinService.update(one,queryWrapper);
            return Result.success(null);

        }else {
            throw new Exceptionbean(20001,"旧密码输入错误");
        }
    }

    @GetMapping("/getemail")
    public Result getemail(HttpServletRequest request){
        String email = request.getHeader("token");
        QueryWrapper<Signin> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq( "email",email);
        Signin one = signinService.getOne(queryWrapper);
        return Result.success(one);
    }


    @ApiOperation("分页查询")
    @PostMapping("{page}/{limit}")
    public Result index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "Signin", value = "查询对象", required = false)
            // required = false传入参数可以为空
            @RequestBody(required = false) Signin signin) {
        //创建page对象，传递当前页和每页记录数
        Page<Signin> pageParam = new Page<>(page, limit);
        //获取条件值
        String email = signin.getEmail();//邮箱
        //封装条件
        QueryWrapper<Signin> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(email)) {
            wrapper.like("email",email);
        }
        wrapper.ne("authority",1);

        //调用方法得到分页查询结果
        IPage<Signin> pageModel = signinService.page(pageParam, wrapper);
        return Result.success(pageModel);
    }

    //根据id获取信息
    @GetMapping("/getidbyinfo/{email}")
    public Result getidbyinfo(@PathVariable String email){

        QueryWrapper<Signin> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("email",email);
        Signin one1 = signinService.getOne(queryWrapper);
        return Result.success(one1);

    }

    //修改
    @PutMapping("/revise")
    public Result revise(@RequestBody Signin signin){
        QueryWrapper<Signin> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("email",signin.getEmail());
        signinService.update(signin,queryWrapper);
        return Result.success(null);

    }

    @ApiOperation("逻辑删除")
    @DeleteMapping("/deleteId/{email}")
    public Result deleteId(@PathVariable String email){
        QueryWrapper<Signin> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("email",email);
       signinService.remove(queryWrapper);
        QueryWrapper<Userinfo> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("email",email);
        userinfoService.remove(queryWrapper1);
        return Result.success(null);

    }


}

